﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;

namespace WindowsGame1.View
{
    class MenuView
    {
        public enum State
        {
            Menu,
            Play,
            Exit,
            Controller
        }

        //Fält
        private SpriteBatch     _spritebatch;
        private KeyboardState   _keyboardState;
        SpriteFont              _font;
        State                   _state = State.Menu;

        //konstruktor som läser in nödvändiga delar 
        public MenuView(GraphicsDeviceManager a_graphicsManager, ContentManager a_contentLoader)
        {
            _spritebatch = new SpriteBatch(a_graphicsManager.GraphicsDevice);
            _font = a_contentLoader.Load<SpriteFont>("SpriteFont1");
        }

        //metod för att rita ut menyn
        public void DrawMenu(Microsoft.Xna.Framework.Graphics.GraphicsDevice a_graphicsDevice)
        {
            a_graphicsDevice.Clear(Color.Black);

            _spritebatch.Begin();

            if (_state == State.Menu)
            {
                DrawMainMenu(a_graphicsDevice);
            }

            else if (_state == State.Controller)
            {
                DrawControllerMenu(a_graphicsDevice);
            }
            
            _spritebatch.End();
        }
        
        //metod som ritar ut huvudmenyn
        private void DrawMainMenu(GraphicsDevice a_graphicsDevice)
        {
            string play =       "Press Enter to Play";
            string controller = "Press Space to view Controller";
            string exit =       "Press Esc to Exit game";

            _spritebatch.DrawString(_font, play, new Vector2(a_graphicsDevice.Viewport.Width / 2 - 100, a_graphicsDevice.Viewport.Height / 2 - 50), Color.White);
            _spritebatch.DrawString(_font, controller, new Vector2(a_graphicsDevice.Viewport.Width / 2 - 100, a_graphicsDevice.Viewport.Height / 2), Color.White);
            _spritebatch.DrawString(_font, exit, new Vector2(a_graphicsDevice.Viewport.Width / 2 - 100, a_graphicsDevice.Viewport.Height / 2 + 50), Color.White);
        }

        //metod som ritar ut kontroller-menyn
        private void DrawControllerMenu(GraphicsDevice a_graphicsDevice)
        {
            string play = "Up - Jump";
            string controller = "Left - Go left";
            string exit = "Right - Go Right";
            string back = "Press Esc to get back to menu";

            _spritebatch.DrawString(_font, play, new Vector2(a_graphicsDevice.Viewport.Width / 2 - 100, a_graphicsDevice.Viewport.Height / 2 - 50), Color.White);
            _spritebatch.DrawString(_font, controller, new Vector2(a_graphicsDevice.Viewport.Width / 2 - 100, a_graphicsDevice.Viewport.Height / 2), Color.White);
            _spritebatch.DrawString(_font, exit, new Vector2(a_graphicsDevice.Viewport.Width / 2 - 100, a_graphicsDevice.Viewport.Height / 2 + 50), Color.White);
            _spritebatch.DrawString(_font, back, new Vector2(a_graphicsDevice.Viewport.Width / 2 - 100, a_graphicsDevice.Viewport.Height / 2 + 100), Color.White);
        }

        //metod som ställer in att man ska visa kontroller-menyn
        internal void ViewControllers()
        {
            _state = State.Controller;
        }

        //metod för att visa huvudmenyn
        internal void ViewMenu()
        {
            _state = State.Menu;
        }

        //Inputs
        public bool UserPressedStartGame()
        {
            Keys enter = Keys.Enter;

            KeyboardState newState = Keyboard.GetState();

            //save state
            _keyboardState = newState;

            //has been pressed and released
            if (_keyboardState.IsKeyDown(enter))
            {
                return true;
            }
            return false;
        }

        public bool UserPressedViewControllers()
        {
            Keys space = Keys.Space;

            KeyboardState newState = Keyboard.GetState();

            //save state
            _keyboardState = newState;

            //has been pressed and released
            if (_keyboardState.IsKeyDown(space))
            {
                return true;
            }
            return false;
        }

        public bool UserWantsToGetBackToMenu()
        {
            Keys Esc = Keys.Escape;

            KeyboardState newState = Keyboard.GetState();

            //save state
            _keyboardState = newState;

            //has been pressed and released
            if (_keyboardState.IsKeyDown(Esc))
            {
                return true;
            }
            return false;
        }
    }
}
